
import { createServer } from 'http'
import { Server } from 'socket.io'

const httpServer = createServer()
const io = new Server(httpServer, { /* options */ })

const rooms = {}
const ROOM_PREFIX = 'room::'

const ioConnectCallback = (socket) => {
  let { roomId, username, isPublisher } = socket.handshake.query
  roomId = ROOM_PREFIX + roomId
  const socketId = socket.id
  console.log(`roomId: ${roomId}, username: ${username}, socketId: ${socketId}, isPublisher: ${isPublisher}`)
  const userInfo = { roomId, username, isPublisher: JSON.parse(isPublisher), socketId }
  // socket.emit('message', { type: 'userInfo', userInfo })
  socket.join(roomId)
  if (rooms[roomId]) {
    rooms[roomId].push(userInfo)
  } else {
    rooms[roomId] = [userInfo]
  }
  const roomUserList = rooms[roomId]
  socket.emit('message', { type: 'userList', user: userInfo, roomUserList })
  socket.to(roomId).emit('message', { type: 'join', user: userInfo, roomUserList: rooms[roomId] })

  socket.on('disconnect', () => {
    const roomUserList = rooms[roomId]
    const index = roomUserList.findIndex(item => item.socketId === socketId)
    const user = roomUserList[index]
    if (index !== -1) {
      roomUserList.splice(index, 1)
    }
    socket.leave(roomId)
    io.to(roomId).emit('message', { type: 'leave', user, roomUserList })
  })

  // socket.on('userList', ({ roomId }) => {
  //   const roomUserList = rooms[roomId]
  //   socket.emit('message', { type: 'userList', roomUserList })
  // })

  socket.on('leave', user => {
    const roomUserList = rooms[user.roomId]
    const index = roomUserList.findIndex(item => item.socketId === user.socketId)
    if (index) {
      roomUserList.splice(index, 1)
    }
    socket.leave(user.roomId)
    io.to(roomId).emit('message', { type: 'leave', user, roomUserList })
  })

  // 发起视频通话
  socket.on('spotVideoCall', ({ from, target }) => {
    socket.to(target.socketId).emit('message', { type: 'spotVideoCall', from, target })
  })

  // 取消视频通话
  socket.on('cancelVideoCall', ({ from, target }) => {
    socket.to(target.socketId).emit('message', { type: 'cancelVideoCall', from, target })
  })

  // 接受视频通话
  socket.on('acceptVideoCall', ({ from, target }) => {
    socket.to(target.socketId).emit('message', { type: 'acceptVideoCall', from, target })
  })

  // 拒绝视频通话
  socket.on('rejectVideoCall', ({ from, target }) => {
    socket.to(from.socketId).emit('message', { type: 'rejectVideoCall', from, target })
  })

  socket.on('videoOffer', ({ from, target, sdp }) => {
    socket.to(target.socketId).emit('message', { type: 'videoOffer', from, target, sdp })
  })

  socket.on('videoAnswer', ({ from, target, sdp }) => {
    socket.to(target.socketId).emit('message', { type: 'videoAnswer', from, target, sdp })
  })

  socket.on('newIceCandidate', ({ from, target, candidate }) => {
    socket.to(target.socketId).emit('message', { type: 'newIceCandidate', from, target, candidate })
  })

  // 挂断
  socket.on('hangUp', ({ from, target }) => {
    socket.to(target.socketId).emit('message', { type: 'hangUp', from, target })
  })
}

io.on('connection', ioConnectCallback)

httpServer.listen(3000)
